package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.service.AdminUserService;
import com.cskaoyan.market.service.AdminUserServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2024/6/17 15:40
 * @Version 1.0
 */
@WebServlet("/admin/user/*")
public class AdminUserController extends HttpServlet {

    AdminUserService adminUserService = new AdminUserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri = req.getRequestURI();
        String op = uri.replace(req.getContextPath() + "/admin/user/", "");
        if ("update".equals(op)) {
            update(req, resp);
        }
    }

    private void update(final HttpServletRequest req, final HttpServletResponse resp) {
        // Enumeration<String> headerNames = req.getHeaderNames();
        // while (headerNames.hasMoreElements()) {
        //     String headerName = headerNames.nextElement();
        //     String headerValue = req.getHeader(headerName);
        //     System.out.println(headerName + ":" + headerValue);
        // }

        try {
            BufferedReader reader = req.getReader();
            String stringBody = reader.readLine();

            String id = JacksonUtil.parseObject(stringBody, "id", String.class);
            String username = JacksonUtil.parseObject(stringBody, "username", String.class);
            String password = JacksonUtil.parseObject(stringBody, "password", String.class);
            String gender = JacksonUtil.parseObject(stringBody, "gender", String.class);
            String lastLoginTime = JacksonUtil.parseObject(stringBody, "lastLoginTime", String.class);
            String lastLoginIp = JacksonUtil.parseObject(stringBody, "lastLoginIp", String.class);
            String userLevel = JacksonUtil.parseObject(stringBody, "userLevel", String.class);
            String nickname = JacksonUtil.parseObject(stringBody, "nickname", String.class);
            String mobile = JacksonUtil.parseObject(stringBody, "mobile", String.class);
            String avatar = JacksonUtil.parseObject(stringBody, "avatar", String.class);
            String weixinOpenid = JacksonUtil.parseObject(stringBody, "weixinOpenid", String.class);
            String sessionKey = JacksonUtil.parseObject(stringBody, "sessionKey", String.class);
            String status = JacksonUtil.parseObject(stringBody, "status", String.class);
            String addTime = JacksonUtil.parseObject(stringBody, "addTime", String.class);
            String updateTime = JacksonUtil.parseObject(stringBody, "updateTime", String.class);
            String deleted = JacksonUtil.parseObject(stringBody, "deleted", String.class);

            MarketUser marketUser = new MarketUser();
            marketUser.setId(Integer.parseInt(id));
            marketUser.setUsername(username);
            marketUser.setPassword(password);
            marketUser.setGender(Byte.parseByte(gender));
            // marketUser.setLastLoginTime(lastLoginTime);
            marketUser.setLastLoginIp(lastLoginIp);
            marketUser.setUserLevel(Byte.parseByte(userLevel));
            marketUser.setNickname(nickname);
            marketUser.setMobile(mobile);
            marketUser.setAvatar(avatar);
            marketUser.setWeixinOpenid(weixinOpenid);
            marketUser.setSessionKey(sessionKey);
            marketUser.setStatus(Byte.parseByte(status));
            // marketUser.setAddTime(addTime);
            // marketUser.setUpdateTime(updateTime);
            marketUser.setDeleted(Boolean.parseBoolean(deleted));


            Integer updateNum = adminUserService.update(marketUser);

            Map<String, Integer> data = new HashMap<>();
            data.put("data", updateNum);
            Object ok = ResponseUtil.ok(data);
            String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
            resp.getWriter().println(s);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/user/", "");
        if ("list".equals(op)) {
            list(req, resp);
        } else if ("detail".equals(op)) {
            detail(req, resp);
        }
    }

    private void detail(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        MarketUser marketUser = adminUserService.detail(id);
        Object o = ResponseUtil.ok(marketUser);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
        resp.getWriter().println(s);
    }

    //需要展示出当前商城的所有用户
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //接收请求参数
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String username = req.getParameter("username");
        String mobile = req.getParameter("mobile");
        List<MarketUser> userList = adminUserService.list(page, limit, sort, order, username, mobile);

        Object o = ResponseUtil.okList(userList);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
        resp.getWriter().println(s);
    }
}
